package nobodyCodesThat;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class FileManagement {
	public static String[] copyNames;

	public static void zipDirectory(String dirPath, String zipName)
			throws IOException {
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
		zipDir(dirPath, zos);
		zos.close();
	}

	private static void zipDir(String dirpath, ZipOutputStream zos)
			throws IOException {
		File f = new File(dirpath);
		String[] flist = f.list();
		for (int i = 0; i < flist.length; i++) {
			File ff = new File(f, flist[i]);
			if (ff.isDirectory()) {
				zipDir(ff.getPath(), zos);
				continue;
			}
			String filepath = ff.getPath();
			ZipEntry entries = new ZipEntry(filepath);
			zos.putNextEntry(entries);
			FileInputStream fis = new FileInputStream(ff);
			int buffersize = 1024;
			byte[] buffer = new byte[buffersize];
			int count;
			while ((count = fis.read(buffer)) != -1) {
				zos.write(buffer, 0, count);
			}
			fis.close();
		}
	}

	public static final int EOF = -1;

	public static void unzipDir(String zipFile) {
		if (zipFile == null)
			System.out.println("Zipfile was null");
		else {
			System.out.print(zipFile + "  ");
			try {
				ZipFile zf = new ZipFile(zipFile);
				for (Enumeration<? extends ZipEntry> e = zf.entries(); e
						.hasMoreElements();) {
					ZipEntry target = e.nextElement();
					saveEntry(zf, target);
				}
			} catch (FileNotFoundException e) {
				System.out.println("zipfile not found");
			} catch (ZipException e) {
				System.out.println("zip error...");
			} catch (IOException e) {
				System.out.println("IO error...");
			}
			System.out.println("unpacked");
		}
	}

	public static void saveEntry(ZipFile zf, ZipEntry target)
			throws ZipException, IOException {
		File file = new File(target.getName());
		if (target.isDirectory())
			file.mkdirs();
		else {
			InputStream is = zf.getInputStream(target);
			BufferedInputStream bis = new BufferedInputStream(is);
			new File(file.getParent()).mkdirs();
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			final int EOF = -1;
			for (int c; (c = bis.read()) != EOF;)
				// oder schneller
				bos.write((byte) c);
			bos.close();
			fos.close();
		}
	}

	public static ArrayList<File> getOldSaveGamesInDir(String sDir) {
		if (null == sDir || 0 >= sDir.trim().length()) {
			System.err.println("Error: directory is null!");
			return null;
		}
		File dir = new File(sDir);
		File[] entries = dir.listFiles();
		if (null == entries)
			System.err.println("'" + sDir + "' is not a valid directory path!");
		if (0 >= entries.length)
			System.err.println("'" + sDir
					+ "' does not contain any files or directories!");
		ArrayList<File> oldSaveFiles = new ArrayList<File>();
		for (int i = 0; i < entries.length; i++) {
			if (!entries[i].isDirectory()
					&& entries[i].getName()
							.contains(Constants.HISTORY_FILENAME)) {
				oldSaveFiles.add(entries[i]);
			}
		}
		return oldSaveFiles;
	}

	public static ArrayList<File> getProfilesInDir(String sDir) {
		if (null == sDir || 0 >= sDir.trim().length()) {
			System.err.println("Error: directory is null!");
			return null;
		}
		File dir = new File(sDir);
		File[] entries = dir.listFiles();
		ArrayList<File> savedProfiles = new ArrayList<File>();
		if (null == entries) {
			System.err.println("'" + sDir + "' is not a valid directory path!");
		} else {
			if (0 >= entries.length) {
				System.err.println("'" + sDir
						+ "' does not contain any files or directories!");
			} else {
				for (int i = 0; i < entries.length; i++) {
					if (entries[i].isDirectory()) {
						savedProfiles.add(entries[i]);
					}
				}
			}
		}
		return savedProfiles;
	}

	public static void getLastModified(String[] files) {
		copyNames = new String[files.length];
		for (int i = 0; i < files.length; i++) {
			File singleFile = new File("" + files[i]);
			long timestamp = singleFile.lastModified();
			Date date = new Date(timestamp);
			System.out.println("Long: " + date.getTime() + "  |  File: "
					+ singleFile.getAbsolutePath());
			SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy  HH:mm:ss");
			String output = sdf.format(date);
			System.out.println("File '" + singleFile.getName()
					+ "' was last modified  " + output);
			copyNames[i] = output;
		}
	}

	public static String getCharacterType(String path) {
		path = path + "\\Player.chr";
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(path);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		StringBuffer stringBuffer = new StringBuffer();
		try {
			while (bufferedReader.read() != -1) {
				stringBuffer.append(bufferedReader.readLine());
			}
			bufferedReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (stringBuffer.substring(48, 55).equalsIgnoreCase("warrior")) {
			return "Warrior";
		} else if (stringBuffer.substring(48, 57).equalsIgnoreCase("sorceress")) {
			return "Sorceress";
		} else {
			return null;
		}
	}
}
